package com.xh.baselibrary.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.CallSuper
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import com.xh.baselibrary.activity.ViewBindingUtil

open class BaseVBFragment<VB : ViewBinding> : Fragment() {

    private var _viewContainer: VB? = null

    @Suppress("MemberVisibilityCanBePrivate")
    protected val mViewContainer: VB get() = _viewContainer!!

    @CallSuper
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return initContentView(inflater, container, savedInstanceState)
    }

    @CallSuper
    protected open fun initContentView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        _viewContainer = ViewBindingUtil.create(this::class.java, inflater, container, false)
        return mViewContainer.root.also { onContentViewInit(it) }
    }

    protected open fun onContentViewInit(contentView: View) {
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _viewContainer = null
    }
}